﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using SnmpForm.SnmpWrapper;

namespace SnmpForm.APConfig
{
    public partial class ucApStatus : ucAPConfig
    {
        public ucApStatus()
        {
            InitializeComponent();
        }

        public override void LoadAp()
        {
            updateApStatusView(m_ap);
        }

        public void cleanApStatusView()
        {
            labelSoftwareVersion.Text = "unknow";
            labelHardwareVersion.Text = "unknow";

            labelWlanConnected.Text = "未知";

            //Lan状态
            labelLanMac.Text = "";
            labelLanNetmask.Text = "";
            labelLanIp.Text = "";

            labelWirelessWorkMode.Text = "";
            labelWirelessEnable.Text = "";
            labelWirelessSSID.Text = "";
            labelWirelessChannel.Text = "";
            labelWirelessChMode.Text = "";
            labelWirelessCwm.Text = "";
            labelWirelessMac.Text = "";
            labelWirelessWds.Text = "unknow";

        }

        private void updateApStatusView(BusinessWrapper.Ap ap)
        {
            cleanApStatusView();

            DbWrapper.DbDataTypes.Ap apinfo = ap.GetInfo();
            DbWrapper.DbDataTypes.WirelessInfo wireinfo = ap.GetApWireless();

            //版本
            labelSoftwareVersion.Text = apinfo.software;
            labelHardwareVersion.Text = apinfo.hardware;

            //Wlan连接状态
            if (wireinfo != null)
            {
                if (wireinfo.enable)
                {
                    labelWlanConnected.Text = "已连接";
                }
                else
                {
                    labelWlanConnected.Text = "未连接";
                }
            }


            //Lan状态
            labelLanMac.Text = apinfo.mac;
            labelLanNetmask.Text = "255.255.255.0";
            labelLanIp.Text = apinfo.ip;

            //Wireless
            if (wireinfo != null)
            {
                switch(wireinfo.workmode){
                    case "enable":
                        labelWirelessWorkMode.Text = "AP ( 接入点模式 )";
                        break;
                    case "disable":
                        labelWirelessWorkMode.Text = "Router ( 路由模式 )";
                        break;
                    default:
                        labelWirelessWorkMode.Text = "未知";
                        break;
                }
                if (wireinfo.enable)
                {
                    labelWirelessEnable.Text = "开启";
                }
                else
                {
                    labelWirelessEnable.Text = "关闭";
                }
                labelWirelessSSID.Text = wireinfo.ssid;
                int nIndex = 0;
                ApConfigItem item = null;

                //信道
                item = AP_DEFINE_DATA.find(AP_DEFINE_DATA.WIRELESS_CHANNALS, wireinfo.channel, out nIndex);
                if (item != null)
                {
                    labelWirelessChannel.Text = item.Text;
                }

                //无线模式
                item = AP_DEFINE_DATA.find(AP_DEFINE_DATA.WIRELESS_MODE, wireinfo.wirelessmode, out nIndex);
                if (item != null)
                {
                    labelWirelessChMode.Text = item.Text;
                }
                //无线频道带宽
                item = AP_DEFINE_DATA.find(AP_DEFINE_DATA.WIRELESS_CWD, wireinfo.channel, out nIndex);
                if (item != null)
                {
                    labelWirelessCwm.Text = item.Text;
                }
                labelWirelessMac.Text = wireinfo.mac;
                if (wireinfo.wds)
                {
                    labelWirelessWds.Text = "开启";
                }
                else
                {
                    labelWirelessWds.Text = "关闭";
                }
            }

            //运行时间
            labelRunningTime.Text = ap.m_strSysRunningTime;
        }

        private void buttonGetRunningTime_Click(object sender, EventArgs e)
        {
            try
            {
                string strRunningTime = m_ap.ScanSystemRunningTime();
                strRunningTime = strRunningTime.Replace("d", "天");
                strRunningTime = strRunningTime.Replace("h", "小时");
                strRunningTime = strRunningTime.Replace("m", "分钟");
                strRunningTime = strRunningTime.Replace("s", "秒");

                labelRunningTime.Text = strRunningTime;
            }
            catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("同步超时");
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("连接异常");
            }
            catch (SnmpException ex)
            {
                MessageBox.Show("代理程序没有提供接口" + ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show(ex.Message, "异常");
            }
        }
    }
}
